﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using FoneBuddy.LinqData;
using FoneBuddy.LinqModel;
using FoneBuddy.LinqModel.Data;

public partial class AddFilter_Detail : System.Web.UI.Page
{
    IDataRepository _dataObject = new DataRepository();

    #region pageload
    protected void Page_Load(object sender, EventArgs e)
    {

        if (!IsPostBack)
        {
            getFilter();
            if (Request.QueryString["Id"] != null)
            {
                lblHeader.Text = "Edit Filter Detail";
                BindData();
            }
            else
            {
                lblHeader.Text = "Add Filter Detail";
            }
        }

    }
    #endregion

    #region save cancel and clear
    protected void Save(object sender, EventArgs e)
    {
        FilterDetail filter = new FilterDetail();
        try
        {
            filter = new FilterDetail(_dataObject); 
            BuildObject(filter);
            filter.GetFilterDetailByName();
         

            if (Request.QueryString["Id"] != null)
            {
                if (filter.Id != Convert.ToInt32(Request.QueryString["Id"]) && filter.Id != 0)
                {
                    divMessage.InnerHtml = "<strong>Error!</strong> Filter Detail already Exists.";
                    divMessage.Attributes.Add("class", "alert alert-danger");
                    return;
                }
                BuildObject(filter);
                filter.Id = Convert.ToInt32(Request.QueryString["Id"]);
                filter.UpdatedDate = System.DateTime.Now;
                filter.IsDeleted = false;
                filter.Update();

                divMessage.InnerHtml = "<strong>Success!</strong> Filter Detail is Updated successfully.";
                divMessage.Attributes.Add("class", "alert alert-success");
            }
            else
            {
                if (filter.Id > 0)
                {
                    divMessage.InnerHtml = "<strong>Error!</strong> Filter Detail already Exists.";
                    divMessage.Attributes.Add("class", "alert alert-danger");
                    return;
                }
                BuildObject(filter);
                filter.CreatedDate = System.DateTime.Now;
                filter.Create();

                divMessage.InnerHtml = "<strong>Success!</strong> Filter Detail is added successfully.";
                divMessage.Attributes.Add("class", "alert alert-success");
                ClearControls();
            }
          
        }
        catch (Exception ex)
        {
        }
    }

    private void ClearControls()
    {
        txtFilterDetail.Text = string.Empty;

    }
    #endregion


    #region binddata
    private void getFilter()
    {
        FilterMaster filter = new FilterMaster();
        List<FilterMaster> filters = new List<FilterMaster>();
        filter = new FilterMaster(_dataObject);

        filters = filter.GetAllFilter();

        if (filters != null)
        {
            drpFilter.DataSource = filters;
            drpFilter.DataBind();
            drpFilter.Items.Insert(0, new ListItem("Select", "0"));
        }
    }
    private void BindData()
    {
        FilterDetail filterDetail = new FilterDetail();
        filterDetail = new FilterDetail(_dataObject);
        filterDetail.Id = Convert.ToInt32(Request.QueryString["Id"]);
        filterDetail.GetFilterDetailById();
        txtFilterDetail.Text = filterDetail.Name; ;
        drpFilter.SelectedValue = filterDetail.FilterId.ToString();
        drpFilter.Enabled = false;
    }
    private void BuildObject(FilterDetail filterDetail)
    {
        filterDetail.Name = txtFilterDetail.Text;
        filterDetail.FilterId = Convert.ToInt32(drpFilter.SelectedValue);
    }
  
    #endregion
}